package com.xinxi11.project.qx.filter;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.GZIPOutputStream;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletResponse;

import com.centfor.core.web.filter.utils.Wrapper;

/**
 * GzipFilter
 * 
 * @author 李国江
 * @date 2010-1-13
 */
public class GzipFilter implements Filter {
	public void init(FilterConfig filterConfig) throws ServletException {
	}

	public void destroy() {
	}

	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		HttpServletResponse resp = (HttpServletResponse) response;
		Wrapper wrapper = new Wrapper(resp);
		chain.doFilter(request, wrapper);
		byte[] gzipData = gzip(wrapper.getResponseData());
		resp.addHeader("Content-Encoding", "gzip");
		resp.setContentLength(gzipData.length);
		ServletOutputStream output = response.getOutputStream();
		output.write(gzipData);
		output.flush();
	}

	private byte[] gzip(byte[] data) {
		ByteArrayOutputStream byteOutput = new ByteArrayOutputStream(10240);
		GZIPOutputStream output = null;
		try {
			output = new GZIPOutputStream(byteOutput);
			output.write(data);
		} catch (IOException e) {
		} finally {
			if (output != null) {
				try {
					output.close();
				} catch (IOException e) {
				}
			}
		}
		return byteOutput.toByteArray();
	}
}
